#include "readconfig.hpp"
#include "logger.h"
#include<fstream>
#include<sstream>

namespace moqi
{
ReadConfig* ReadConfig::_pInstence = nullptr;  
ReadConfig::AutoRelease ReadConfig::_ar;

ReadConfig*ReadConfig:: getInstance()
{
    if (_pInstence == nullptr)
    {
        _pInstence = new ReadConfig();
    }
    return _pInstence;
}

void ReadConfig:: deleteInstance() 
{
    if (_pInstence != nullptr) 
    {
        delete _pInstence;
    }
}

void ReadConfig::read(const string& filename) 
{
    cout << "read config\n";
    ifstream ifs(filename);
    if (!ifs.good())
    {
        cerr << ("open config file fail\n");
        return;
    }
    string line;
    string key, value;
    while (getline(ifs, line)) 
    {
        istringstream iss(line);
        //字符串输入流默认以空格为分隔符
        iss >> key >> value;
        _configPairs[key] =  value;
    }
}

string ReadConfig::get(const string& key) const{
    //auto it = _configPairs.begin();
    for (auto & it: _configPairs) {
        if (it.first== key)
            return it.second;
    }
    static string s;
    return s;
}

ReadConfig::ReadConfig()
{

}

ReadConfig::~ReadConfig() 
{

}

}//end of namespace moqi
